package com.xm.xmapp.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;

import com.xm.xmapp.R;
import com.xm.xmapp.app.CodeConstant;
import com.xm.xmapp.base.MyBaseAdapter;
import com.xm.xmapp.base.ViewHolder;
import com.xm.xmapp.customview.NameDialog;
import com.xm.xmapp.myinterface.MyDialogButton;
import com.xm.xmapp.tools.Tools;

public class CarGridViewAdapter extends MyBaseAdapter<String[]> {

	private int select = 0;
	private Activity content;

	public int getSelect() {
		return select;
	}

	public void setContent(Activity content) {
		this.content = content;
	}

	public void setSelect(int select) {
		this.select = select;
	}

	public CarGridViewAdapter(ArrayList<String[]> list, Activity activity, int layoutId) {
		super(list, activity, layoutId);

	}

	@Override
	public void setValues(final ViewHolder holder, final String[] t, final int position) {

		if (select == position) {
			holder.getView(R.id.money).setBackgroundResource(R.mipmap.list_xuanzhong_nor);
		} else {
			holder.getView(R.id.money).setBackgroundResource(R.mipmap.weiixuanzhong);
		}

		if (position < (list.size() - 1)) {

			if (t[1] != null)
				holder.setText(R.id.money, "充" + t[0] + "送" + t[1]);
			else
				holder.setText(R.id.money, t[0] + "元");

		} else {
			if (t[0] == null || t[0].equals(""))
				holder.setText(R.id.money, "其它");
			else
				holder.setText(R.id.money, t[0] + "元");

		}

		holder.getConvertView().setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

				if (select != position) {
					select = position;
					notifyDataSetChanged();
				}

				if (position == (list.size() - 1)) {

					final NameDialog sexAndNameDialog = new NameDialog(content, "请输入充值金额", new MyDialogButton() {

						@Override
						public void setTitle(int flag, String str) {

							try {
								double money = Double.parseDouble(str);
								if (money >= CodeConstant.MIN_CAR_FROZEN_PAY) {
									holder.setText(R.id.money, str + "元");
									t[0] = str;
								} else {
									t[0] = null;
									holder.setText(R.id.money, "其它");
									Tools.showMessage("充值金额最少为"+CodeConstant.MIN_CAR_FROZEN_PAY+"元");
								}

								if (money > CodeConstant.MAX_CAR_FROZEN_PAY) {
									t[0] = null;
									holder.setText(R.id.money, "其它");
									Tools.showMessage("充值金额太大");
								}

							} catch (Exception e) {
								t[0] = null;
								holder.setText(R.id.money, "其它");
								Tools.showMessage("请输入充值金额");

							}
						}
					}, InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
					sexAndNameDialog.show();
				}

			}
		});
	}
}
